package net.zoneland.knowledge.utils;

import org.apache.commons.collections4.CollectionUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import net.zoneland.knowledge.constant.FieldConstant;
import net.zoneland.knowledge.model.QaQueryDto;

/**
 * 这个类要修改两个地方，不同版本的ES都有.
 * interface不想引入es依赖，没办法复用
 *
 * @author: bcc
 * 2021/5/26
 */
public final class EsQueryUtils {

    /**
     * 匹配部门题库.
     * es数据中带orgDeptDn的为部门题库;
     * 要达到的效果：
     * flag 99 表示全省通用数据。
     * (
     *      (
     *          orgDeptDn is null
     *          and
     *          flag in (falg , '99')
     *
     *       )
     *       or
     *       orgDeptDn in ('aa','bb')
     * )
     * @param boolqueryBuilder
     * @param requestParms
     */
    public static void appendDeptQuery(final BoolQueryBuilder boolqueryBuilder, final QaQueryDto requestParms) {
        //修改说明：考虑到兼职部门有可能跨公司，通用题库需要匹配flag，部门题库只需要匹配orgDeptDn不再判断flag
        BoolQueryBuilder shouldQueryBuilderForOrgDentDn = QueryBuilders.boolQuery();
        shouldQueryBuilderForOrgDentDn.mustNot(QueryBuilders.existsQuery(FieldConstant.FIELD_ORGDEPTDN));
        shouldQueryBuilderForOrgDentDn.must(QueryBuilders.termsQuery(FieldConstant.FIELD_FLAG, requestParms.getFlags()));
        BoolQueryBuilder shouldQueryBuilder2 = QueryBuilders.boolQuery();
        shouldQueryBuilder2.should(shouldQueryBuilderForOrgDentDn);
        if (CollectionUtils.isNotEmpty(requestParms.getOrgDeptDnList())) {
            shouldQueryBuilder2.should(QueryBuilders.termsQuery(FieldConstant.FIELD_ORGDEPTDN, requestParms.getOrgDeptDnList()));
        }
        boolqueryBuilder.must(shouldQueryBuilder2);
    }

    /**
     * 权限控制逻辑:
     * （
     *      (
     *      userIds is null
     *      or
     *      userIds in ('')
     *      )
     *      or
     *      (
     *      deptIds is  null
     *      or
     *      deptIds in ('')
     *      )
     *      or
     *      (
     *      roleFlags is null
     *      or
     *      roleFlags in ('')
     *      )
     * ）
     * @param boolqueryBuilder
     * @param requestParms
     */
    public static void appendRoleQuery(final BoolQueryBuilder boolqueryBuilder, final QaQueryDto requestParms) {
        //权限控制查询
        BoolQueryBuilder shouldQueryBuilderForUserIds = QueryBuilders.boolQuery();
        shouldQueryBuilderForUserIds.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(FieldConstant.FIELD_USERIDS)));
        shouldQueryBuilderForUserIds.should(QueryBuilders.termsQuery(FieldConstant.FIELD_USERIDS, requestParms.getUserIds()));

        BoolQueryBuilder shouldQueryBuilderForDeptIds = QueryBuilders.boolQuery();
        shouldQueryBuilderForDeptIds.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(FieldConstant.FIELD_DEPTIDS)));
        shouldQueryBuilderForDeptIds.should(QueryBuilders.termsQuery(FieldConstant.FIELD_DEPTIDS, requestParms.getDeptIds()));

        BoolQueryBuilder shouldQueryBuilderForRoleFlags = QueryBuilders.boolQuery();
        shouldQueryBuilderForRoleFlags.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(FieldConstant.FIELD_ROLEFLAGS)));
        shouldQueryBuilderForRoleFlags.should(QueryBuilders.termsQuery(FieldConstant.FIELD_ROLEFLAGS, requestParms.getRoleFlags()));

        BoolQueryBuilder shouldQueryBuilderAll = QueryBuilders.boolQuery();
        shouldQueryBuilderAll.should(shouldQueryBuilderForUserIds);
        shouldQueryBuilderAll.should(shouldQueryBuilderForDeptIds);
        shouldQueryBuilderAll.should(shouldQueryBuilderForRoleFlags);
        boolqueryBuilder.must(shouldQueryBuilderAll);
    }

}
